
package com.xifan.myaccount.util;

import android.util.SparseArray;
import android.util.SparseIntArray;

import com.xifan.myaccount.entity.RecordDetail;
import com.xifan.myaccount.entity.RecordType;
import com.xifan.myaccount.entity.TypeStats;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DataAnalyzer {

    private List<RecordDetail> list;
    private float totlMoney;

    public DataAnalyzer(List<RecordDetail> list) {
        this.list = list;
        totlMoney = 0f;
    }

    public List<TypeStats> getAnalyzedTypes() {
        int opacity = list.size();
        SparseIntArray typeArray = new SparseIntArray(opacity);
        SparseArray<Float> typeMoney = new SparseArray<Float>();
        for (int i = 0; i < opacity; i++) {
            RecordDetail detail = list.get(i);
            typeArray.put(detail.getRecordType(), typeArray.get(detail.getRecordType(), 0) + 1);
            typeMoney.put(detail.getRecordType(),
                    typeMoney.get(detail.getRecordType(), 0f).floatValue() + detail.getRawMoney());
            totlMoney += detail.getRawMoney();
        }

        List<TypeStats> statsList = new ArrayList<TypeStats>();
        List<RecordType> typeList = SmartType.getTypeList(list.get(0).getOperateType());
        for (int i = 0; i < typeArray.size(); i++) {
            TypeStats stats = new TypeStats();
            RecordType tmpInfo = SmartType.getTypeInfoById(typeList, typeArray.keyAt(i));
            stats.setTypeId(typeArray.keyAt(i));
            stats.setTypeName(tmpInfo.typeName);
            stats.setTypeMoney(typeMoney.valueAt(i));
            stats.setPercentage((int) (stats.getTypeMoney() / totlMoney * 100));
            statsList.add(stats);
        }
        Collections.sort(statsList);
        return statsList;
    }

    public float getTotlMoney() {
        return totlMoney;
    }
}
